/*
 *@本JS功能：考试班级管理
 *@本js作者：杨锡君
 *@编写时间：2018年05月23日
 */
$(function() {
    var Alert1 = new Alert();
    var confirm1 = new Confirm();
    var loading1 = new Loading();
    loading1.init();
    var $tableBox = $(".tableBox");
    var th = "<tr>" +
        "<th class=\"col-lg-1\"><input type=\"checkbox\" name=\"controlAll\" id=\"controlAll\" class=\"allCheck\"></th>" +
        "<th class=\"col-lg-7\">班级名称</th>" +
        "<th class=\"col-lg-2\">班级/申请班状态</th>" +
        "<th class=\"col-lg-2\">考试状态</th>" +
        "</tr>";

    function tabelModel(opt) {
        var str = "";
        var string = "";
        var str1 = "";
        if(opt.classExamState == 0){
            str = "未考试";
        }
        if(opt.classExamState == 1){
            str = "已考试";
        }
        if(opt.classExamState == 2){
            str = "考试中";
        }
        if(opt.examState == 0){
            str1 = "未考试";
        }
        if(opt.examState == 1){
            str1 = "已考试";
        }

        if(opt.classExamState == 0 || opt.classExamState == 1  ){
            string = "<input data-classid=" + opt.classId + " value=\"0\" name=\"selecteds\" type=\"checkbox\" class=\"oneCheck\" />";
        }
        return "<tr data-num=\"0\">" +
            "<td class=\"col-lg-1\">"+ string +"</td>" +
            "<td class=\"col-lg-7\" title=" + opt.className + ">" + opt.className + "</td>" +
            "<td class=\"col-lg-2\">"+ str +"</td>" +
            "<td class=\"col-lg-2\">" + str1 + "</td>" +
            "</tr>";
    }
    var classMana = {
        init: function() {
            this.bindEvent();
        },
        bindEvent: function() {
            var self = this;
            $(".iconfont").html(result.paramData.courseTitle);
            $(".courseName").html(result.paramData.courseName);
            $(".textName").html("试卷:"+result.paramData.textName);
            self.tableList();
            $(".testBegin").off("click").click(function () {
                self.delListEvent();
            });
            $(".looktgs").off("click").click(function () {
                var json = JSON.stringify({
                    "pageName":"testGenerateList",
                    "paramData":{
                        "courseId":result.paramData.courseId,
                        "courseName":result.paramData.courseName,
                        "courseTitle":result.paramData.courseTitle
                    }
                });
                window.location.href ="skipPage?jsonStr="+json;
            });
        },
        tableList: function() {
            var self = this;
            loading1.show();
            var json = JSON.stringify({"courseId":parseInt(result.paramData.courseId),"onlinequestionId":parseInt(result.paramData.onlinequestionId)});
            $.ajax({
                type: "post",
                url: "getAllClassStateByTeaId",
                async: true,
                data:{
                  "jsonStr":json
                },
                success: function(msg) {
                    loading1.hide();
                    msg = JSON.parse(msg);
                    $tableBox.empty();
                    if(msg.classList.length != 0) {
                        //列表
                        var tabel = "";
                        var url = msg.url;
                        $.map(msg.classList, function(opt) {
                            tabel += tabelModel(opt,url);
                        });
                        $tableBox.append(th + tabel);
                        self.allCheckEvent();
                    } else {
                        var trImg = "<tr><td colspan='3'><img style='position:relative;margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
                        $tableBox.append(th + trImg)
                    }

                },
                error:function () {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;
                }
            });

        },
        delListEvent: function() {
            var self = this;
            if($(".oneCheck[value=1]").length == 0) {
                Alert1.content = "请选择要考试的班级";
                Alert1.init();
                Alert1.show();
            } else {
                confirm1.content = "确定对选中的班级开始考试吗？";
                confirm1.success = function () {
                    var list = [];
                    for(var i = 0; i < $(".oneCheck[value=1]").length; i++) {
                        list.push({"classId":parseInt($(".oneCheck[value=1]").eq(i).attr("data-classid")),"examMark":2});
                    }
                    for(var i = 0; i < $(".oneCheck[value=0]").length; i++) {
                        list.push({"classId":parseInt($(".oneCheck[value=0]").eq(i).attr("data-classid")),"examMark":0});
                    }
                    var jsons = JSON.stringify({"courseId":parseInt(result.paramData.courseId),"onlinequestionId":parseInt(result.paramData.onlinequestionId),"list":list});
                    loading1.show();
                    $.ajax({
                        type: "post",
                        url: "startExame",
                        data: {
                            "jsonStr":jsons
                        },
                        success: function(msg){
                            loading1.hide();
                            msg = JSON.parse(msg);
                            if(msg.resultFlag == true){
                                self.tableList();
                            }else{
                                Alert1.content = msg.msg;
                                Alert1.init();
                                Alert1.show();
                            }
                        },
                        error:function () {
                            loading1.hide();
                            var json = JSON.stringify({
                                "pageName": "error"
                            });
                            window.location.href = "skipPage?jsonStr=" + json;
                        }
                    });
                };
                confirm1.init();
                confirm1.show();




            }
        },
        allCheckEvent: function() {
            //全选
            allCheck.init("controlAll", "oneCheck");
        }
    };
    classMana.init();
});